package com.kotlin.myapplication.mvp.presenter

import com.kotlin.myapplication.mvp.model.UserContract
import com.kotlin.myapplication.utils.Logger
import com.kotlin.myapplication.network.RetrofitUtil
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers

/**
 *@lil
 *@创建时间 2019/11/18 17:45
 *@描述
 */


class UserPresenter(val view: UserContract.view) : UserContract.userModelResult {

    /**
     * 订阅管理
     */
    var compositeDisposable: CompositeDisposable

    init {
        view.presenter = this
        compositeDisposable = CompositeDisposable()

    }

    /**
     * 登录
     */
    override fun login(userId: String, password: String) {

        val disposable = RetrofitUtil
                .userService
                .login(userId, password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ result ->
//                    if(result.code)
                    view.showUserModel(result)
                    Logger.log(result.toString())
                }, { error ->
                    view.error(error.message.toString())
                    Logger.log(error.message.toString())
                })

        compositeDisposable.add(disposable)


    }

    /**
     * 开启订阅
     */
    override fun subscribe() {

    }

    /**
     * 关闭订阅
     */
    override fun unsubscribe() {
        compositeDisposable.clear()
    }


}